package com.example.fragment;

import android.nfc.Tag;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ListView;

import com.example.Adapter.LeftMenuAdapter;
import com.example.api.ApiService;
import com.example.interfaces.ResponseCallback;
import com.example.shopapp.R;
import com.example.viewmodel.CatalogData;
import com.example.viewmodel.LeftMenu;
import com.example.viewmodel.ResponseData;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link CatalogFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class CatalogFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private ListView catalog_left;
    List<LeftMenu> leftMenus;
    private LeftMenuAdapter leftMenuAdapter;
    private LeftMenu currentCategory;

    //右边
    private FrameLayout frameLayout;
    private List<Fragment> fragmentList = new ArrayList<>();


    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public CatalogFragment() {
        // Required empty public constructor
    }

    public void initView(View view){
        catalog_left = view.findViewById(R.id.catalog_left);
    }

    public void selectItem(int index) {
        FragmentManager fragmentManager = getChildFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        Log.e("TAG", "数据:....."+ this.leftMenus.size());
        for (int i = 0; i < this.leftMenus.size(); i++) {
            this.leftMenus.get(i).setChecked(false);
            //所有的fragment先进行隐藏
            fragmentTransaction.hide(fragmentList.get(i));
        };
        this.leftMenus.get(index).setChecked(true);
        //对应的fragment,显示
        fragmentTransaction.show(fragmentList.get(index));
        fragmentTransaction.commit();
//        数据变化,通过notifyDataSetChanged 通知组件刷新
        leftMenuAdapter.notifyDataSetChanged();
    }

    public void initLeftMenu() {
        catalog_left.setAdapter(leftMenuAdapter);
        catalog_left.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                selectItem(i);
            }
        });
    }

    public void addFragment(){
        FragmentManager fragmentManager = getChildFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        // 如果是在fragment里面 FragmentTransaction ,getParentFragmentManager().beginTransaction()
//        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        //有10条数据,就动态创建10个fragment
        for (int i = 0; i < leftMenus.size(); i++) {
            int id = leftMenus.get(i).getId();
            // 在这里可以给每一个fragment携带参数
            CatalogContentFragment catalogFragment = CatalogContentFragment.newInstance(id+"","");
            fragmentList.add(catalogFragment);
            fragmentTransaction.add(R.id.catalog_right, fragmentList.get(i));
        }
        fragmentTransaction.commit();
        selectItem(0);
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment CatalogFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static CatalogFragment newInstance(String param1, String param2) {
        CatalogFragment fragment = new CatalogFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_catalog, container, false);//获取layout中的fragment_home.xml
        initView(view);

        ApiService.GetCatalog(new ResponseCallback<ResponseData<CatalogData>>() {
            @Override
            public void onSuccess(ResponseData<CatalogData> callback) {
                leftMenus = callback.getData().getCategoryList();//获取数据

                leftMenuAdapter = new LeftMenuAdapter(view.getContext(), leftMenus);
                initLeftMenu();

                addFragment();

            }
        });

        return view;
    }
}